package com.softdev.pojo;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import com.softdev.common.AbstractBaseEntity;
import io.swagger.annotations.ApiModel;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.springframework.format.annotation.DateTimeFormat;

import javax.persistence.*;
import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@ApiModel("项目")
@Setter
@Getter
@AllArgsConstructor
@NoArgsConstructor
@Entity
@Table(name = "projects")
public class Project extends AbstractBaseEntity implements Serializable {

    private static final long serialVersionUID = 5943423721698608912L;

    @Column(name = "name")
    private String name;

    @Column(name = "type")
    private int type;

    @Column(name = "background")
    private String background;

    @ManyToMany(fetch = FetchType.LAZY , cascade = CascadeType.PERSIST)
    @JoinTable(name = "projects_skills" ,
            joinColumns = {@JoinColumn(name = "project_id")} ,
            inverseJoinColumns = {@JoinColumn(name = "skill_id")})
    private List<Skill> skills;

    @Column(name = "begin_time",columnDefinition = "timestamp")
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm") //此注解用来接收字符串类型的参数封装成LocalDateTime类型
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm",timezone = "GMT+8", shape = JsonFormat.Shape.STRING) //此注解将date类型数据转成字符串响应出去
    @JsonDeserialize(using = LocalDateTimeDeserializer.class)		// 反序列化
    @JsonSerialize(using = LocalDateTimeSerializer.class)		// 序列化
    private LocalDateTime beginTime;

    @Column(name = "finish_time",columnDefinition = "timestamp")
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm") //此注解用来接收字符串类型的参数封装成LocalDateTime类型
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm",timezone = "GMT+8", shape = JsonFormat.Shape.STRING) //此注解将date类型数据转成字符串响应出去
    @JsonDeserialize(using = LocalDateTimeDeserializer.class)		// 反序列化
    @JsonSerialize(using = LocalDateTimeSerializer.class)		// 序列化
    private LocalDateTime finishTime;

    @Column(name = "status")
    private int status;

    @Column(name = "stages")
    private String stages;

    @Column(name = "cur")
    private Integer cur;

    @OneToMany(fetch = FetchType.EAGER , cascade = CascadeType.PERSIST)
    private Set<Module> modules;

    @OneToMany(fetch = FetchType.LAZY , mappedBy = "project")
    private Set<ProjectRiskMapping> risks;

    public int updateCur() {

        List<Integer> curs = this.modules.stream().map(Module::getCur)
                .collect(Collectors.toList());

        Comparator<Integer> comparator = Comparator.comparing(Integer::intValue);
        this.cur = curs.stream().min(comparator).get();

        return this.status;
    }

    public int updateStatus() throws JsonProcessingException {

        ObjectMapper objectMapper = new ObjectMapper();

        List<String> stageList = objectMapper.readValue(this.stages, new TypeReference<List<String>>() {});
        int total = stageList.size();

        this.status = this.cur == total ? this.status + 1 : this.status;

        return this.status;
    }

}
